<?php

namespace common\models\datas;

use Yii;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%type}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $setting
 * @property string $description
 * @property integer $sort
 * @property integer $is_show
 */
class Type extends \common\components\bases\BaseModel
{
    public $brand_id;
    public $backend;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%type}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['sort', 'is_show'], 'integer'],
            [['name'], 'string', 'max' => 100],
            [['order_operateclass'], 'string'],
            [['name'], 'required'],
            ['name', 'unique','message'=>'此类型已存在'],
            [['setting', 'description'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自动编号'),
            'name' => Yii::t('common', '类型名称'),
            'setting' => Yii::t('common', '设置'),
            'description' => Yii::t('common', '描述'),
            'sort' => Yii::t('common', '排序'),
            'is_show' => Yii::t('common', '是否显示'),
            'brand_id' => Yii::t('common', '品牌'),
            'backend' => Yii::t('common', '后台添加模块'),
        ];
    }

    public function extraFields(){
        return [
            'count'=> function(){
                return isset($this->attr)?count($this->attr):0;
            },
            'operate' => function(){
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
           }
        ];
    }

    public function getCates(){
        return $this->hasMany(Cate::className(),['type_id'=>'id']);
    }

    public function getAttr(){

        return $this->hasMany(Attribute::className(),['type_id'=>'id']);
    }
}
